package com.bobi.ecofavor.controller;

import com.bobi.ecofavor.common.base.R;
import com.bobi.ecofavor.common.enums.ResponseCodeEnum;
import com.bobi.ecofavor.common.request.LoginModel;
import com.bobi.ecofavor.common.utils.JsonResponse;
import com.bobi.ecofavor.model.domain.User;
import com.bobi.ecofavor.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 功能描述
 *
 * @author Mars.wq [wqexpore@163.com]
 * @version 1.0
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    public ILoginService loginService;

    /**
     * 1.微信通过code登录 获取openId和sessionKey
     *
     * @param code code
     * @return R
     */
    @GetMapping("/doLogin")
    public R doLogin(String code, User user) {
        return R.success(loginService.doLogin(code, user));
    }


    /**
     * 2.获取手机号
     *
     * @param request 请求参数
     * @return R
     */
    @PostMapping("/getPhoneNumber")
    public R getPhoneNumber(@RequestBody LoginModel request) throws Exception {
        return R.success(loginService.getPhoneNumber(request));
    }

    /**
     * 3.更新用户信息
     *
     * @param request 请求参数
     * @return R
     */
    @PostMapping("/updateUserInfo")
    public R updateUserInfo(@RequestBody LoginModel request) {
        loginService.updateUserInfo(request);
        return R.success();
    }

    /**
     * 初始化登录信息
     */
    @GetMapping("/initUserInfo")
    public JsonResponse initUserInfo(LoginModel request) {
        LoginModel loginModel = loginService.initUserInfo(request);
        // 如果没有注册
        JsonResponse<Object> success = JsonResponse.success(null);
        if (loginModel == null) {
            success.setCode(ResponseCodeEnum.UNREGISTERED.getCode());
            success.setMessage(ResponseCodeEnum.UNREGISTERED.getMsg());
            return success;
        }
        return success.setData(loginModel);
    }


    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public JsonResponse logout() {
        return loginService.logout();
    }

}
